package com.tttare.haoyigou.sellers.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttare.haoyigou.mapper.TbSpecificationOptionMapper;
import com.tttare.haoyigou.mapper.TbTypeTemplateMapper;
import com.tttare.haoyigou.pojo.TbSpecificationOption;
import com.tttare.haoyigou.pojo.TbSpecificationOptionExample;
import com.tttare.haoyigou.pojo.TbTypeTemplateExample.Criteria;
import com.tttare.haoyigou.pojo.TbTypeTemplate;
import com.tttare.haoyigou.pojo.TbTypeTemplateExample;
import com.tttare.haoyigou.sellers.service.TypeTemplateService;

import entity.PageResult;

@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {

	@Autowired
	private TbTypeTemplateMapper tbTypeTemplateMapper;
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Override
	public List<TbTypeTemplate> findAll() {
		List<TbTypeTemplate> list = tbTypeTemplateMapper.selectByExample(null);
		return list;
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbTypeTemplate> pageContent = (Page<TbTypeTemplate>)tbTypeTemplateMapper.selectByExample(null);
		PageResult<TbTypeTemplate> result = new PageResult<TbTypeTemplate>(
				pageContent.getTotal(),pageContent.getResult());
		return result;
	}

	@Override
	public void add(TbTypeTemplate typeTemplate) {
		tbTypeTemplateMapper.insert(typeTemplate);
	}

	@Override
	public void update(TbTypeTemplate typeTemplate) {
		tbTypeTemplateMapper.updateByPrimaryKey(typeTemplate);

	}

	@Override
	public TbTypeTemplate findOne(Long id) {
		TbTypeTemplate typeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
		return typeTemplate;
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			tbTypeTemplateMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public PageResult findPage(TbTypeTemplate typeTemplate, int pageNum,
			int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbTypeTemplateExample example=new TbTypeTemplateExample();
		Criteria criteria = example.createCriteria();
		
		if(typeTemplate!=null){			
						if(typeTemplate.getName()!=null && typeTemplate.getName().length()>0){
				criteria.andNameLike("%"+typeTemplate.getName()+"%");
			}
			if(typeTemplate.getSpecIds()!=null && typeTemplate.getSpecIds().length()>0){
				criteria.andSpecIdsLike("%"+typeTemplate.getSpecIds()+"%");
			}
			if(typeTemplate.getBrandIds()!=null && typeTemplate.getBrandIds().length()>0){
				criteria.andBrandIdsLike("%"+typeTemplate.getBrandIds()+"%");
			}
			if(typeTemplate.getCustomAttributeItems()!=null && typeTemplate.getCustomAttributeItems().length()>0){
				criteria.andCustomAttributeItemsLike("%"+typeTemplate.getCustomAttributeItems()+"%");
			}
	
		}
		
		Page<TbTypeTemplate> page= (Page<TbTypeTemplate>)tbTypeTemplateMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	@Autowired
	private TbSpecificationOptionMapper tbSpecificationOptionMapper;

	@Override
	public List<Map> findSpecList(Long id) {
		//通过模板id得到specIds
		TbTypeTemplate template = tbTypeTemplateMapper.selectByPrimaryKey(id);
		String specIds = template.getSpecIds();
		//specIds是一个List<Map>形式的string,需要转化
		List<Map> list = JSON.parseArray(specIds,Map.class);//parseArray,第二个参数,是数组元素的泛型,即List<Map>中的map
		for (Map map : list) {
			//获得specId
			int specId = (int) map.get("id");
			TbSpecificationOptionExample example=new TbSpecificationOptionExample();
			com.tttare.haoyigou.pojo.TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
			criteria.andSpecIdEqualTo(new Long(specId));
			List<TbSpecificationOption> specificationOptions = tbSpecificationOptionMapper.selectByExample(example);
			map.put("specificationOptions", specificationOptions);
		}
		return list;
		
	}
	
	//使用Redis做缓存,将品牌和规格存入redis
	public void saveToRedis(){
		List<TbTypeTemplate> list = findAll();
		//存入品牌和规格
		for (TbTypeTemplate tbTypeTemplate : list) {
			List<Map> specList = JSON.parseArray(tbTypeTemplate.getSpecIds(),Map.class);
			redisTemplate.boundHashOps("specList").put(tbTypeTemplate.getId(),specList);
			List<Map> brandList = JSON.parseArray(tbTypeTemplate.getBrandIds(),Map.class);
			redisTemplate.boundHashOps("brandList").put(tbTypeTemplate.getId(),brandList);
			
		}
	}
	

}
